<?php
declare (strict_types=1);

namespace app\admin\controller;

use addons\BuilderPlus\components\Button;
use addons\BuilderPlus\components\Column as Col;
use addons\BuilderPlus\components\Search;
use addons\BuilderPlus\facade\Table;
use app\admin\validate\OrderDeliver;
use app\common\model\Area;
use app\common\tool\Result;
use think\exception\ValidateException;
use think\facade\View;

class Order extends Base
{
    public function getOptions(): array
    {
        return [
            'pk'     => 'id',
            'model'  => \app\common\model\Order::class,
            'with'   => ['user', 'express', 'p', 'c', 'd', 'productsField'],
            'append' => ['statusText'],
            'fast'   => [
                'edit' => 'order/edit'
            ]
        ];
    }

    public function index(): string
    {

        return Table::init($this)
            ->addColumns([
                ['id', '订单ID', fn(Col $col) => $col->width(80)],
                ['sn', '订单号', fn(Col $col) => $col->width(200)],
                ['uid', '下单会员', fn(Col $col) => $col->width(250)->templet('#_userTpl')],
                ['product', '订单商品', fn(Col $col) => $col->minWidth(300)->templet('#productTpl')],
                ['', '下单地址', fn(Col $col) => $col->templet('#addressTpl')],
                ['real_amount', '实际支付', fn(Col $col) => $col->width(120)],
                ['pay_time', '支付时间', fn(Col $col) => $col->width(140)],
                ['pay_way', '支付方式', fn(Col $col) => $col->width(120)],
                ['statusText', '订单状态', fn(Col $col) => $col->width(90)->fixed('right')]
            ])
            ->addTopButtons(['deleteAll'])
            ->addRightButtons([
                'edit'    => fn(Button $btn) => $btn->title('详情'),
                'deliver' => fn(Button $btn) => $btn->title('发货')
                    ->layerTitle('发货详情')
                    ->url(url('order/deliver')->build())
                    ->event('layer')
                    ->width('1000px')
            ], 150, 'orderToolBar')
            ->addSearch([
                ['uid', '会员编号/昵称/手机号', fn($s) => $s->relation('\app\common\model\User', 'code|nickname|mobile,id')],
                ['id', '商品名称', fn($s) => $s->relation('\app\common\model\OrderProduct', 'title,order_id')],
                ['province-city-district', '下单地址', fn(Search $s) => $s->type('linkage')->linkageUrl(url('order/getArea')->build())],
                ['sn', '订单编号'],
                ['status', '订单状态', fn(Search $s) => $s->type('select')
                    ->optionValue(\app\common\model\Order::getOrderStatus('', 2))]
            ])
            ->enableExport()
            ->addTpl(['order'])
            ->specifyQuery([
                ['status', 'not in', [1, 7]]
            ])
            ->fetch();
    }

    public function deliver()
    {
        if ($this->request->isPost()) {
            try {
                $param = $this->request->post();
                validate(OrderDeliver::class)->check($param);
                return Result::fastReturn($this->model::deliver($param, $this->admin));
            } catch (ValidateException $e) {
                return Result::fail($e->getError());
            }
        }
        View::assign([
            'express' => \app\common\model\Express::where(['status' => 1])->select(),
            'info'    => $this->model::where('id', $this->request->get('id'))->find()
        ]);
        return View::fetch();
    }

    public function getArea()
    {
        $param = $this->request->get();
        return Result::success('success', Area::getArea($param['id'] ?? 0, $param['level']));
    }

}
